<template>
  <div class="wrap">
      <div>
        <!--选取截面-->
        <label class="label">{{$t('message.SectionAttributeTool.sectionPlane')}}:</label>
        <query-list
          v-model="params.sectionPlaneElements"
          :description="$t('message.SectionAttributeTool.SectionAttributeTool')"
          :active="params.queryListActiveMap.sectionPlaneElements"
          @switchQuery="switchQuery('sectionPlaneElements')"
          :tipMsg="$t('message.SectionAttributeTool.selectSectionPlane')"
          @removeItem="removeSelect">
        </query-list>
      </div>
    <template v-if="workspaceMode === 'PartDocument'">
      <div>
        <!--选取截取的实体对象-->
        <label class="label">{{$t('message.SectionAttributeTool.cutEntity')}}</label>
        <query-list
          v-model="params.sectionEntityElements"
          :description="$t('message.SectionAttributeTool.cutEntity')"
          :active="params.queryListActiveMap.sectionEntityElements"
          @switchQuery="switchQuery('sectionEntityElements')"
          :tipMsg="$t('message.SectionAttributeTool.selectCutEntity')"
          @removeItem="removeSelect">
        </query-list>
      </div>
    </template>

    <div v-if="sectionAttributeInfo" class="border sectionAttributeMessageBox" tabindex="-1">
      <section-attribute-info-content v-if="!!sectionAttributeInfo"
                                      :section-attribute-info="sectionAttributeInfo"/>
    </div>
    <!--    确认和关闭-->
    <div class="btn">
      <el-button type="primary" size="small"
                 :disabled="!isComputed"
                 @click="computeSectionAttribute">{{ $t('message.SectionAttributeTool.compute') }}
      </el-button>
      <el-button
        size="small"
        @click="close">
        {{ $t('message.WingTwistDesignDlg.cancel') }}
      </el-button>
    </div>
  </div>
</template>

<script>
import DialogMixin from "../DialogMixin";
import {ElementType, EntityType, GeomType} from "../../../modeling/TypeDefine";
import {sectionAttributeCommand} from "../../../Command/SectionAttributeCommand";
import {sectionAttributeToolManager} from "./SecitonAttributeToolManager";
import SectionAttributeInfoContent from "./SectionAttributeInfoContent.vue";
import {surfaceDeformCommand} from "../../../Command/SurfaceDeformCommand";

export default {
  components: {SectionAttributeInfoContent},
  mixins: [DialogMixin],
  data() {
    return {
      params: {
        sectionPlaneElements:[],
        sectionEntityElements:[],
        queryListActiveMap:{
          sectionPlaneElements:1,
          sectionEntityElements:0,
        }
      },
      isComputed:false,
      sectionAttributeInfo: null,//截面属性列表
      workspaceMode:'PartDocument', //当前激活文档是装配还是零件
    };
  },

  mounted() {
    sectionAttributeCommand.init(400,300,300,() => {
      sectionAttributeCommand.handlePickElement(elements => {
        if(this.params.queryListActiveMap.sectionPlaneElements){
          if(elements.element && elements.element.length > 0){
            this.params.sectionPlaneElements = [];
            elements.element.forEach(element => {
              if((element.element.type === ElementType.FACE || element.element.type === ElementType.SURFACE) && element.element.params.surfaceType === GeomType.PLANE){
                //如果是在装配模式下，自己对名称和实例进行组合
                if(element.instance && element.element.name){
                  element.element.name += "@" + element.instance.name;
                  element.element.instanceInfo = element.instance;
                }

                this.params.sectionPlaneElements.push(element.element);
                return;
              }
            })
          }
        }else if(this.params.queryListActiveMap.sectionEntityElements){
          if(elements.element && elements.element.length > 0){
            this.params.sectionEntityElements = [];
            elements.element.forEach(subElement => {
              let entityId = subElement.element.params?.entityRefId;
              if (entityId) {
                sectionAttributeCommand.getEntitiesByIds([entityId], entities => {
                  if(entities.length > 0){
                    let entity = entities[0];
                    if(entity.type === EntityType.SOLID) {
                      this.params.sectionEntityElements.push(entity);
                      return;
                    }
                    sectionAttributeCommand.highLightEntityOutline([entityId]);
                  }
                });
              }
            })
          }
        }
      })

      sectionAttributeCommand.getWorkspaceMode(null, (data) => {
        this.workspaceMode = data;
      });
    })
  },

  methods: {
    changePluginContainerHeight() {
      sectionAttributeCommand.changePluginContainerHeight({minWidth: this.$el.clientWidth, maxHeight: this.$el.clientHeight});
    },
    close(){
      sectionAttributeCommand.closeActiveCmd();
    },
    //移除高亮
    removeSelect(item) {
      if (this.workspaceMode === 'PartDocument') {
        sectionAttributeCommand.clearHighLightElements([item?.elementId ? item.elementId : item.id]);
      } else {
        let paramId = {
          instanceId: item.instanceInfo.id,
          elementId: item.elementId,
        }
        sectionAttributeCommand.clearHighLightInstanceElement(paramId);
      }
      this.sectionAttributeInfo = null;
    },
    //计算属性
    computeSectionAttribute() {
      if(this.params.sectionPlaneElements.length === 0){
        return;
      }
      let sectionEntityElements = this.params.sectionEntityElements.length > 0 ? this.params.sectionEntityElements[0] : null;
      let param = {
        //用于进行切除的平面
        cutPlane:this.params.sectionPlaneElements[0],
        //用于进行切除的实体
        cutEntity:sectionEntityElements,
      }

      sectionAttributeCommand.computeSectionAttributeInfo(param, (data) => {
        //返回结果的处理
        if(data?.attributeInfos === null){
          sectionAttributeCommand.eventBusAlertMsg("截面属性计算出现错误，请检查选择是否正确。","error");
        } else {
          sectionAttributeToolManager.parseSectionAttributeInfos(data.attributeInfos);
          this.sectionAttributeInfo = sectionAttributeToolManager.sectionAttributeInfo;
        }
      });
    },

    //激活的选取框
    switchQuery(name) {
      for (const key in this.params.queryListActiveMap) {
        if (key === name){
          this.params.queryListActiveMap[key] = 1;
        }else{
          this.params.queryListActiveMap[key] = 0;
        }
      }
    },

    //是否能够进行截面属性计算
    beComputed(){
      //每次当输入要素发生变化，清空输出信息框
      this.sectionAttributeInfo = null;
      if(this.params.sectionPlaneElements.length > 0){
        this.isComputed = true;
      }else{
        this.isComputed = false;
      }
    },
  },
  watch: {
    "params.sectionPlaneElements": function () {
      this.beComputed();
    },
    "params.sectionEntityElements": function () {
      this.beComputed();
    },
  },
};
</script>
<style scoped>
.wrap {
    font-size: 12px;
}
.result-item .angle-icon {
    font-weight: bold;
    padding: 0 2px;
}
.dialog-query-list .move-buttons {
    margin-right: 35px;
}
.sectionAttributeMessageBox {

}
</style>
